package com.hnqcgczy.supplier.controller;

import com.hnqcgczy.supplier.service.BankService;
import com.hnqcgczy.supplier.service.impl.BankServiceImpl;
import com.hnqcgczy.supplier.utils.BaseServlet;
import com.hnqcgczy.supplier.vo.Bank;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "BankServlet", value = "/BankServlet")
public class BankServlet extends BaseServlet {
    private BankService bankService = new BankServiceImpl();


    public void addBank(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        //获取前台的值封装到实体对象送到服务层
        Bank bank = new Bank();
        System.out.println("company_id"+request.getParameter("company_id"));
        bank.setCompanyId(request.getParameter("company_id") != null ? Integer.parseInt(request.getParameter("company_id")) : 0);
        bank.setBankDeposit(request.getParameter("bank_deposit"));
        bank.setProvince(request.getParameter("province"));
        bank.setSubBranch(request.getParameter("sub_branch"));
        bank.setCity(request.getParameter("city"));
        bank.setAccountName(request.getParameter("account_name"));
        bank.setBankAccount(request.getParameter("bank_account"));
        //
        boolean flag = bankService.add(bank);
        response.sendRedirect("BankServlet");
    }

    public void updateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {

        //获取前台的值封装到实体对象送到服务层
        Bank bank = new Bank();
        bank.setBankId(request.getParameter("bank_id") != null ? Integer.parseInt(request.getParameter("bank_id")) : 0);
        bank.setCompanyId(request.getParameter("company_id") != null ? Integer.parseInt(request.getParameter("company_id")) : 0);
        bank.setBankDeposit(request.getParameter("bank_deposit"));
        bank.setProvince(request.getParameter("province"));
        bank.setSubBranch(request.getParameter("sub_branch"));
        bank.setCity(request.getParameter("city"));
        bank.setAccountName(request.getParameter("account_name"));
        bank.setBankAccount(request.getParameter("bank_account"));
        //
        boolean flag = bankService.update(bank);
        response.sendRedirect("BankServlet");
    }

    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        int id = request.getParameter("bank_id") != null ? Integer.parseInt(request.getParameter("bank_id")) : 0;
        boolean flag = bankService.delete(id);

        response.sendRedirect("BankServlet");
    }


    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        //bank_deposit,bank_account
        String bankDeposit = request.getParameter("bankDeposit") != null ? request.getParameter("bankDeposit") : "";
        String bankAccount = request.getParameter("bankAccount") != null ? request.getParameter("bankAccount") : "";
        Bank query = new Bank();
        query.setBankDeposit(bankDeposit);
        query.setBankAccount(bankAccount);


        int pageNow = request.getParameter("pageNow") != null ? Integer.parseInt(request.getParameter("pageNow")) : 1;
        int pageSize = 10;

        int totals = bankService.totals(query);
        if (pageNow <= 1) {
            pageNow = 1;
        }
        if (pageNow >= totals) {
            pageNow = totals;
        }
        System.out.println("pagenow"+pageNow);
        List<Bank> banks = bankService.findByLike(query, pageNow, pageSize);
        int pages = totals % pageSize == 0 ? totals / pageSize : totals / pageSize + 1;
        //存
        request.setAttribute("pageNow", pageNow);
        request.setAttribute("banks", banks);
        request.setAttribute("totals", totals);
        request.setAttribute("pages", pages);
        //跳
        request.getRequestDispatcher("CollectionInfo.jsp").forward(request, response);
    }


}
//postman测试请同学们自行完成